<template>
  <div>
    <el-dialog v-bind="$attrs" :visible="open" width="1000px" @close="onClose" title="订单同步">
        <el-form ref="elForm" :model="formData" :rules="rules" size="medium" label-width="100px">
          <el-row :gutter="10">
            <el-col :span="8">
              <el-form-item label="订单来源" prop="syncSource">
                <el-select v-model="formData.syncSource" placeholder="请选择订单来源" clearable @change="changeSource">
                  <el-option v-for="(item, index) in syncSourceOptions" :key="index" :label="item.label" :value="item.value" :disabled="item.disabled"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="8">
              <el-form-item label="下单时间在" prop="syncTime">
                <el-date-picker type="datetime" v-model="formData.syncTime" format="yyyy-MM-dd HH:mm"
                                value-format="yyyy-MM-dd HH:mm" :style="{width: '100%'}" placeholder="下单时间"
                                clearable></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="16">
              之后的订单 （上次同步最新下单时间：{{syncTime}}）
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="8">
              <el-form-item label="平台账号" prop="platformAccount">
                <el-input v-model="formData.platformAccount" placeholder="请输入平台账号" clearable
                          :style="{width: '100%'}"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="平台密码" prop="platformPassword">
                <el-input v-model="formData.platformPassword" placeholder="请输入平台密码" clearable type="password"
                          :style="{width: '100%'}"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label-width="0" >
                <el-button type="primary" size="medium" @click="handelConfirm"> 开始同步</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
    </el-dialog>
  </div>
</template>
<script>
import {syncData,getConfig} from "@/api/ota/ctrip";

export default {
  inheritAttrs: false,
  props: {
    open: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      formData: {
        id:null,
        syncSource: 1,
        syncTime: null,
        platformAccount: undefined,
        platformPassword: undefined,
      },
      configList:[],
      syncTime:null,
      rules: {
        syncSource: [{
          required: true,
          message: '请选择订单来源',
          trigger: 'change'
        }],
        syncTime: [{
          required: true,
          message: '同步范围不能为空',
          trigger: 'change'
        }],
        platformAccount: [{
          required: true,
          message: '请输入平台账号',
          trigger: 'blur'
        }],
        platformPassword: [{
          required: true,
          message: '请输入平台密码',
          trigger: 'blur'
        }],
      },
      syncSourceOptions: [{
        "label": "携程",
        "value": 1
      }, {
        "label": "悟空",
        "value": 2
      }, {
        "label": "飞猪",
        "value": 3
      }],
    }
  },
  computed: {},
  watch: {},
  created() {
    getConfig().then(res=>{
      this.configList=res.data
      this.changeSource()
    })
  },
  mounted() {

  },
  methods: {
    changeSource() {
     let config= this.configList.filter(item=>{
        return item.syncSource==this.formData.syncSource
      })
      if(config&&config[0]){
        this.formData.id= config[0].id;
        this.formData.syncTime= config[0].syncTime;
        this.syncTime= config[0].syncTime;
        this.formData.platformAccount= config[0].platformAccount;
        this.formData.platformPassword= config[0].platformPassword;
      }else{
        this.formData.id= null;
        this.formData.syncTime= null;
        this.syncTime= null;
        this.formData.platformAccount= null;
        this.formData.platformPassword= null;
      }
    },
    onClose() {
      this.$refs['elForm'].resetFields()
      this.close()
    },
    close() {
      this.$emit('close', false)
    },
    handelConfirm() {
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          this.$modal.loading("同步中...")
          syncData(this.formData).then(res => {
            this.$modal.msgSuccess(res.msg);
            this.$modal.closeLoading()
            this.close()
          }).catch(r=>{
            this.$modal.closeLoading()
          })
        }
      })
    },
  }
}

</script>
<style>
</style>
